﻿<%@page contentType="text/html; charset=utf-8" language="java" import="cn.ac.cintcm.synonymous.*"  errorPage="" %>
<%@page import="java.util.Collection, java.util.Iterator, java.util.List, java.util.Date" %>


<% 
String dictFile = Config.getDictFile();
if(dictFile==null) {
   response.sendRedirect("upload.html");
}
String start = request.getParameter("p");
String w = Util.getUrlParameter(Util.unescape(request.getQueryString()),"word");
String aliasWord = Util.getUrlParameter(Util.unescape(request.getQueryString()),"a");
String type = request.getParameter("t"); 
String author = request.getRemoteAddr();

Dictionary dict = CSVDictionary.getInstance(dictFile);
DataSource ds = CSVDataSource.createDataSource(Config.getDataSourceFile());
EditCtl editCtl = EditCtl.getEditCtlInstance(ds, dict);

Message msg  = null;

if (type != null && type.equals("sd")) {
	   	msg = editCtl.save(); 
} 

Word word = dict.get(w); 

if (application.getAttribute("count") == null) {
	application.setAttribute("count", new Long(Word.getCounter()));
}  

Long counter = (Long) application.getAttribute("count");

if (w != null && !w.trim().equals("")) {
  if ( type != null) {
	  if (type.charAt(0) == EditCtl.ADD_INFORMAL) {
	    if(word != null) {
		    Word aliasword = new Word();
		    aliasword.setName(aliasWord);
		    aliasword.setAlias(true);
		    aliasword.setCreated(new Date());
		    aliasword.setAuthor(author);
		    counter = new Long(counter.longValue() + 1L);
		    aliasword.setId(counter.longValue());
		    msg = editCtl.addWord(word, aliasword);
	    }
	  } else if (type.charAt(0) == EditCtl.DELETE_DICT_WORD) {
	   	msg = editCtl.deleteWord(w);
	  }
	}
}


application.setAttribute ("count", counter);

PagedData  pWords = new PagedWords(dict.search(w));
DataSourceCtl  dctl = new DataSourceCtl(pWords); 

String urls = "dict.jsp?";
if (start==null) { 
	start = "1";
}
String navigation = dctl.getNavigation(urls, start);
List list = dctl.getData(start); 

word = dict.get(w); 

%>

<html>
<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type">
  <title>正异名修正_词典修正</title>
  <script language="javascript">
<!-- Copyright 2006,2007 Bontrager Connection, LLC
// http://bontragerconnection.com/ and http://www.willmaster.com/
// Version: July 28, 2007
var cX = 0; var cY = 0; var rX = 0; var rY = 0;
var selectedWord = " ";
function UpdateCursorPosition(e){ 
	cX = e.pageX; 
	cY = e.pageY;
}
function UpdateCursorPositionDocAll(e){ 
	cX = event.clientX; 
	cY = event.clientY;
}
if(document.all) { 
	document.onmousemove = UpdateCursorPositionDocAll; 
}else {
	document.onmousemove = UpdateCursorPosition; 
}
function AssignPosition(d) {
	if(self.pageYOffset) {
		rX = self.pageXOffset;
		rY = self.pageYOffset;
	} else if(document.documentElement && document.documentElement.scrollTop) {
		rX = document.documentElement.scrollLeft;
		rY = document.documentElement.scrollTop;
	}	else if(document.body) {
		rX = document.body.scrollLeft;
		rY = document.body.scrollTop;
	}
	if(document.all) {
		cX += rX; 
		cY += rY;
	}
	d.style.left = (cX+10) + "px";
	d.style.top = (cY+10) + "px";
}

function HideContent(d) {
	if(d.length < 1) {
		 return; 
	}
	document.getElementById(d).style.display = "none";
}
function ShowContent(d, word) {
	if(d.length < 1) { 
		return; 
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
	selectedWord  = word;
}
function ReverseContentDisplay(d) {
	if(d.length < 1) { 
		return; 
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if(dd.style.display == "none") {
		 dd.style.display = "block"; 
	} else { 
		 	dd.style.display = "none";
	}
}

function showItems(id) {
 window.onbeforeunload  = null;
 window.location = "<%=urls%>p=<%=start%>&s=" + id;
}

function navigation(page) {
	window.location.href = "<%=urls%>p="+page + "&word=" + escape('<%=w%>');
}
function search(event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var fname =  document.getElementById("word");
		window.location.href ="dict.jsp?word="+escape(fname.value);
  }
}

function addAlia() {
	window.onbeforeunload  = null;
	var informal = document.getElementById("informalWord"); 
	if (formal.value !=  selectedWord) {
	    window.location = "<%=urls%>p=<%=start%>&t=<%=EditCtl.ADD_INFORMAL%>&a=" +escape(informal.value) + "&word=" + escape(selectedWord);
	}
}   

function deleteWord() {
	window.onbeforeunload  = null;
	window.location = "<%=urls%>p=<%=start%>&t=<%=EditCtl.DELETE_DICT_WORD%>&word=" + escape(selectedWord);
	
}
function view() {
	window.location.href ="dict.jsp?word="+escape(selectedWord);
}

function save() {
	window.onbeforeunload  = null;
	window.location.href = "<%=urls%>p=<%=start%>&t=sd";
} 

  </script>
</head>
<body>
<div id="formal" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="deleteWord(); return true;" href="#">删除</a><br>
<a onClick="addAlia();" href="#">增加异名词</a><input id="informalWord" value=""><br>
<a onClick="view();" href="#">查看</a><br>
<div onClick="HideContent('formal'); return true;">关闭</div>
</div> 

<div id="informal" style="display:none; position:absolute; width: 150px; border-style: solid; background-color: #C0C0C0; padding: 2px;">
<a onClick="view();" href="#">查看</a><br>
<a onClick="deleteWord(); return true;" href="#">删除</a><br>
<div onClick="HideContent('informal'); return true;">关闭</div>
</div> 

<table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td>输入查询词：<input id="word" value="<%=(w==null?"":w)%>" onkeypress="search(event);"><%=(msg==null?"":msg.toString())+ editCtl.getEditMessage()%>&nbsp;<a href="data/<%=Config.getDictFile()%>">下载词典</a></td>
    </tr>
<%
 if (word != null) {
%>
    <tr>
      <td>
      <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td>词</td>
            <td>类别</td>
            <td><%=word.isAlias()?Config.FORMAL:Config.INFORMAL%></td>
         </tr>
          <tr>
            <td><a onClick="ShowContent('<%=word.isAlias()?"informal":"formal"%>', '<%=word.getName()%>');" href="#"><%=word.getName()%></a></td>
            <td><%=word.isAlias()?Config.INFORMAL:Config.FORMAL%></td>
            <td>
     <%
     if (word.isAlias()) {
          out.print(word.getFormalName());
     } else {
     	  Collection alias = word.getAlias();
     	  if (alias != null) {
		     	  Iterator i = alias.iterator();
		     	  String name  ="";
		     	  if(i.hasNext()) {
		     	      name = ((Word)i.next()).getName() ;
                out.print("<a onClick=\"ShowContent('informal','" + name + "');\" href=\"#\">"+name+"</a>");
		     	  }
		     	  while(i.hasNext()) {
		     	      out.print(Config.FIELD_SEPERATOR );
		     	      name = ((Word)i.next()).getName() ;
                out.print("<a onClick=\"ShowContent('informal','" + name + "');\" href=\"#\">"+name+"</a>");
            }
       }
     }
    %>
          </td>
         </tr>
        </tbody>
      </table>
      </td>
    </tr>
<%
  }
%>
<%
 if (list.size() > 0) {
%>
    <tr>
      <td>
      <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td colspan=2><%=navigation%></td>
          </tr>
          <tr>
            <td>词</td>
            <td>类别</td>
          </tr>
     <%     String[] data = null;
            for (int i = 0; i < list.size(); i++) {
						  data = (String[]) list.get(i);
     %>
          <tr>
            <td><a onClick="ShowContent('<%=data[1].equals(Config.INFORMAL)?"informal":"formal"%>', '<%=data[0]%>');" href="#"><%=data[0]%></a></td>
            <td><%=data[1]%></td>
          </tr>
     <% 
           }
     %>
        </tbody>
      </table>
      </td>
    </tr>
<%
  }
%>    

  </tbody>
</table>
<br>
</body>
</html>
